home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-07-19  |  37KB  |  1,578 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Modifications for Chapter 13
  4.  *
  5.  * Implementation of the CPatronDoc derivation of CDocument that
  6.  * manages pages for us.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include <memory.h>
  19. #include "patron.h"
  20.  
  21.  
  22.  
  23. /*
  24.  * CPatronDoc::CPatronDoc
  25.  * CPatronDoc::~CPatronDoc
  26.  *
  27.  * Constructor Parameters:
  28.  *  hInst           HINSTANCE of the application.
  29.  */
  30.  
  31. CPatronDoc::CPatronDoc(HINSTANCE hInst)
  32.     : CDocument(hInst)
  33.     {
  34.     m_pPG=NULL;
  35.     m_lVer=VERSIONCURRENT;
  36.     m_pIStorage=NULL;
  37.     m_fPrintSetup=TRUE;
  38.     m_pDropTarget=NULL;
  39.  
  40.     m_cfEmbeddedObject   =RegisterClipboardFormat(CF_EMBEDDEDOBJECT);
  41.     m_cfObjectDescriptor =RegisterClipboardFormat(CF_OBJECTDESCRIPTOR);
  42.     m_cfLinkSource       =RegisterClipboardFormat(CF_LINKSOURCE);
  43.     m_cfLinkSrcDescriptor=RegisterClipboardFormat(CF_LINKSRCDESCRIPTOR);
  44.     m_fShowTypes         =FALSE;
  45.  
  46.     //CHAPTER13MOD
  47.     m_cRef               =1L;
  48.     m_fRename            =TRUE;
  49.     m_dwRegROT           =0L;
  50.     m_pIPersistFile      =NULL;
  51.     m_pIOleItemContainer =NULL;
  52.     //End CHAPTER13MOD
  53.  
  54.     return;
  55.     }
  56.  
  57.  
  58. CPatronDoc::~CPatronDoc(void)
  59.     {
  60.     //CHAPTER13MOD
  61.     if (0L!=m_dwRegROT)
  62.         OleStdRevokeAsRunning(&m_dwRegROT);
  63.  
  64.     if (NULL!=m_pIPersistFile)
  65.         delete m_pIPersistFile;
  66.  
  67.     if (NULL!=m_pIOleItemContainer)
  68.         delete m_pIOleItemContainer;
  69.     //End CHAPTER13MOD
  70.  
  71.     if (NULL!=m_pDropTarget)
  72.         {
  73.         RevokeDragDrop(m_hWnd);
  74.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, FALSE, TRUE);
  75.         m_pDropTarget->Release();
  76.         }
  77.  
  78.     if (NULL!=m_pPG)
  79.         delete m_pPG;
  80.  
  81.     if (NULL!=m_pIStorage)
  82.         m_pIStorage->Release();
  83.  
  84.     CoFreeUnusedLibraries();
  85.  
  86.     return;
  87.     }
  88.  
  89.  
  90.  
  91. //CHAPTER13MOD
  92.  
  93. /*
  94.  * CPatronDoc::QueryInterface
  95.  * CPatronDoc::AddRef
  96.  * CPatronDoc::Release
  97.  *
  98.  * Purpose:
  99.  *  IUnknown members for CPatronDoc object.
  100.  */
  101.  
  102. STDMETHODIMP CPatronDoc::QueryInterface(REFIID riid, LPLPVOID ppv)
  103.     {
  104.     *ppv=NULL;
  105.  
  106.     /*
  107.      * WARNING:  If you are using multiple inheritance, like I am
  108.      * with CPatronDoc, you'd better be sure to typecast "this" into
  109.      * an IUnknown to point to the right function table.  In developing
  110.      * this I forgot, and the AddRef call below ended up in the
  111.      * destructor!  Wrong function table!
  112.      */
  113.  
  114.     if (IsEqualIID(riid, IID_IUnknown))
  115.         *ppv=(LPVOID)(LPUNKNOWN)this;
  116.  
  117.     if (IsEqualIID(riid, IID_IOleItemContainer)
  118.         || IsEqualIID(riid, IID_IOleContainer)
  119.         || IsEqualIID(riid, IID_IParseDisplayName))
  120.         *ppv=(LPVOID)m_pIOleItemContainer;
  121.  
  122.     if (IsEqualIID(riid, IID_IPersistFile) || IsEqualIID(riid, IID_IPersist))
  123.         *ppv=(LPVOID)m_pIPersistFile;
  124.  
  125.     if (NULL!=*ppv)
  126.         {
  127.         ((LPUNKNOWN)*ppv)->AddRef();
  128.         return NOERROR;
  129.         }
  130.  
  131.     return ResultFromScode(E_NOINTERFACE);
  132.     }
  133.  
  134.  
  135. STDMETHODIMP_(ULONG) CPatronDoc::AddRef(void)
  136.     {
  137.     return ++m_cRef;
  138.     }
  139.  
  140. STDMETHODIMP_(ULONG) CPatronDoc::Release(void)
  141.     {
  142.     ULONG           cRefT;
  143.  
  144.     cRefT=--m_cRef;
  145.  
  146.     if (0L==m_cRef)
  147.         {
  148.         /*
  149.          * Telling ourselves to WM_CLOSE will inform the frame that will
  150.          * ask the client to CloseDocument which will delete us when
  151.          * appropriate.  We then call ObjectDestroyed in our destructor
  152.          * to initiate full shutdown.
  153.          */
  154.         PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  155.         }
  156.  
  157.     return cRefT;
  158.     }
  159. //End CHAPTER13MOD
  160.  
  161.  
  162.  
  163. /*
  164.  * CPatronDoc::FInit
  165.  *
  166.  * Purpose:
  167.  *  Initializes an already created document window.  The client actually
  168.  *  creates the window for us, then passes that here for further
  169.  *  initialization.
  170.  *
  171.  * Parameters:
  172.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  173.  *
  174.  * Return Value:
  175.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  176.  */
  177.  
  178. BOOL CPatronDoc::FInit(LPDOCUMENTINIT pDI)
  179.     {
  180.     //Change the stringtable range to our customization.
  181.     pDI->idsMin=IDS_DOCUMENTMIN;
  182.     pDI->idsMax=IDS_DOCUMENTMAX;
  183.  
  184.     //Do default initialization
  185.     if (!CDocument::FInit(pDI))
  186.         return FALSE;
  187.  
  188.     //Pages are created when we get a ::ULoad later.
  189.  
  190.     //CHAPTER13MOD
  191.     //Instantiate our interfaces
  192.     m_pIPersistFile=new CImpIPersistFile(this, (LPUNKNOWN)this);
  193.  
  194.     if (NULL==m_pIPersistFile)
  195.         return FALSE;
  196.  
  197.     m_pIOleItemContainer=new CImpIOleItemContainer((LPVOID)this
  198.         , (LPUNKNOWN)this, TRUE);
  199.  
  200.     if (NULL==m_pIOleItemContainer)
  201.         return FALSE;
  202.  
  203.     //End CHAPTER13MOD
  204.  
  205.     return TRUE;
  206.     }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. /*
  214.  * CPatronDoc::FMessageHook
  215.  *
  216.  * Purpose:
  217.  *  Processes WM_SIZE for the document so we can resize the Pages window.
  218.  *
  219.  * Parameters:
  220.  *  <WndProc Parameters>
  221.  *  pLRes           LRESULT FAR * in which to store the return value
  222.  *                  for the message.
  223.  *
  224.  * Return Value:
  225.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  226.  */
  227.  
  228. BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  229.     , LPARAM lParam, LRESULT FAR *pLRes)
  230.     {
  231.     UINT        dx, dy;
  232.     RECT        rc;
  233.  
  234.     if (WM_SIZE==iMsg && NULL!=m_pPG)
  235.         {
  236.         dx=LOWORD(lParam);
  237.         dy=HIWORD(lParam);
  238.  
  239.         //Resize the Pages window to fit the new client area of the document
  240.         GetClientRect(hWnd, &rc);
  241.         m_pPG->RectSet(&rc, FALSE);
  242.         }
  243.  
  244.     /*
  245.      * We return FALSE even on WM_SIZE so we can let the default procedure
  246.      * handle maximized MDI child windows appropriately.
  247.      */
  248.     return FALSE;
  249.     }
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. /*
  259.  * CPatronDoc::Clear
  260.  *
  261.  * Purpose:
  262.  *  Sets all contents in the document back to defaults with no filename.
  263.  *
  264.  * Paramters:
  265.  *  None
  266.  *
  267.  * Return Value:
  268.  *  None
  269.  */
  270.  
  271. void CPatronDoc::Clear(void)
  272.     {
  273.     //Completely reset the pages
  274.     if (NULL!=m_pPG)
  275.         m_pPG->FIStorageSet(NULL, FALSE, FALSE);
  276.  
  277.     CDocument::Clear();
  278.     m_lVer=VERSIONCURRENT;
  279.     return;
  280.     }
  281.  
  282.  
  283.  
  284.  
  285. /*
  286.  * CPatronDoc::FDirtyGet
  287.  *
  288.  * Purpose:
  289.  *  Returns the current dirty status of the document.
  290.  *
  291.  * Parameters:
  292.  *  None
  293.  *
  294.  * Return Value:
  295.  *  BOOL            TRUE if the file is clean, FALSE otherwise.
  296.  */
  297.  
  298. BOOL CPatronDoc::FDirtyGet()
  299.     {
  300.     BOOL    fPageDirty=FALSE;
  301.  
  302.     if (NULL!=m_pPG)
  303.         fPageDirty=m_pPG->FIsDirty();
  304.  
  305.     return m_fDirty | fPageDirty;
  306.     }
  307.  
  308.  
  309.  
  310.  
  311.  
  312. /*
  313.  * CPatronDoc::Delete
  314.  *
  315.  * Purpose:
  316.  *  Removed the current object from the document.
  317.  *
  318.  * Paramters:
  319.  *  None
  320.  *
  321.  * Return Value:
  322.  *  None
  323.  */
  324.  
  325. void CPatronDoc::Delete(void)
  326.     {
  327.     if (NULL!=m_pPG)
  328.         m_pPG->TenantDestroy();
  329.  
  330.     CoFreeUnusedLibraries();
  331.     return;
  332.     }
  333.  
  334.  
  335.  
  336. /*
  337.  * CPatronDoc::FQueryPrinterSetup
  338.  *
  339.  * Purpose:
  340.  *  Returns whether or not the Printer Setup menu item can be
  341.  *  enabled.  Once you create a tenant in any page, Printer Setup
  342.  *  is voided simply to keep this sample simple, that is, we don't
  343.  *  have to worry about reorganizing potentially large amounts
  344.  *  of layout after we start plopping down objects.
  345.  *
  346.  * Parameters:
  347.  *  None
  348.  *
  349.  * Return Value:
  350.  *  BOOL            TRUE to enable the menu, FALSE otherwise.
  351.  */
  352.  
  353. BOOL CPatronDoc::FQueryPrinterSetup(void)
  354.     {
  355.     return m_fPrintSetup;
  356.     }
  357.  
  358.  
  359.  
  360.  
  361.  
  362. /*
  363.  * CPatronDoc::FQueryObjectSelected
  364.  *
  365.  * Purpose:
  366.  *  Returns whether or not there is an object selected in this
  367.  *  document for Cut, Copy, Delete functions.
  368.  *
  369.  * Parameters:
  370.  *  hMenu           HMENU of the Edit menu.
  371.  *
  372.  * Return Value:
  373.  *  BOOL            TRUE if we have an object, FALSE otherwise.
  374.  */
  375.  
  376. BOOL CPatronDoc::FQueryObjectSelected(HMENU hMenu)
  377.     {
  378.     if (NULL!=m_pPG)
  379.         return m_pPG->FQueryObjectSelected(hMenu);
  380.  
  381.     return FALSE;
  382.     }
  383.  
  384.  
  385.  
  386.  
  387.  
  388. /*
  389.  * CPatronDoc::ULoad
  390.  *
  391.  * Purpose: